<template>
  <div class="p-2">
    <transition :enter-active-class="searchAnimate.enter" :leave-active-class="searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item :label="t('view.billRule-name')"  prop="name">
            <el-input v-model="queryParams.name" :placeholder="t('common.inputText')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item :label="t('view.billRule-category')"  prop="category">
            <el-input v-model="queryParams.category" :placeholder="t('common.inputText')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item :label="t('view.billRule-enCode')"  prop="enCode">
            <el-input v-model="queryParams.enCode" :placeholder="t('common.inputText')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item :label="t('view.billRule-prefix')"  prop="prefix">
            <el-input v-model="queryParams.prefix" :placeholder="t('common.inputText')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item :label="t('view.billRule-dateFormat')"  prop="dateFormat">
            <el-input v-model="queryParams.dateFormat" :placeholder="t('common.inputText')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item :label="t('view.billRule-digit')"  prop="digit">
            <el-input v-model="queryParams.digit" :placeholder="t('common.inputText')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item :label="t('view.billRule-startNumber')"  prop="startNumber">
            <el-input v-model="queryParams.startNumber" :placeholder="t('common.inputText')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item :label="t('view.billRule-example')"  prop="example">
            <el-input v-model="queryParams.example" :placeholder="t('common.inputText')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item :label="t('view.billRule-thisNumber')"  prop="thisNumber">
            <el-input v-model="queryParams.thisNumber" :placeholder="t('common.inputText')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item :label="t('view.billRule-outputNumber')"  prop="outputNumber">
            <el-input v-model="queryParams.outputNumber" :placeholder="t('common.inputText')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item :label="t('view.billRule-description')"  prop="description">
            <el-input v-model="queryParams.description" :placeholder="t('common.inputText')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item :label="t('view.billRule-seq')"  prop="seq">
            <el-input v-model="queryParams.seq" :placeholder="t('common.inputText')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">{{t('common.searchText')}}</el-button>
            <el-button icon="Refresh" @click="resetQuery">{{t('common.resetText')}}</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:billRule:add']">{{t('common.addText')}}</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['system:billRule:edit']">{{t('common.editText')}}</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['system:billRule:remove']">{{t('common.delText')}}</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:billRule:export']">{{t('common.exportText')}}</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="billRuleList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column :label="t('view.billRule-id')"  align="center" prop="id" v-if="true" />
        <el-table-column :label="t('view.billRule-name')"  align="center" prop="name" />
        <el-table-column :label="t('view.billRule-category')"  align="center" prop="category" />
        <el-table-column :label="t('view.billRule-enCode')"  align="center" prop="enCode" />
        <el-table-column :label="t('view.billRule-prefix')"  align="center" prop="prefix" />
        <el-table-column :label="t('view.billRule-dateFormat')"  align="center" prop="dateFormat" />
        <el-table-column :label="t('view.billRule-digit')"  align="center" prop="digit" />
        <el-table-column :label="t('view.billRule-startNumber')"  align="center" prop="startNumber" />
        <el-table-column :label="t('view.billRule-example')"  align="center" prop="example" />
        <el-table-column :label="t('view.billRule-thisNumber')"  align="center" prop="thisNumber" />
        <el-table-column :label="t('view.billRule-outputNumber')"  align="center" prop="outputNumber" />
        <el-table-column :label="t('view.billRule-description')"  align="center" prop="description" />
        <el-table-column :label="t('view.billRule-seq')"  align="center" prop="seq" />
        <el-table-column :label="t('common.operatingText')" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip :content="t('common.editText')" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:billRule:edit']"></el-button>
            </el-tooltip>
            <el-tooltip :content="t('common.delText')" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:billRule:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改单据规则对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="billRuleFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单据名称" prop="name">
          <el-input v-model="form.name" :placeholder="t('common.inputText')" />
        </el-form-item>
        <el-form-item label="分类" prop="category">
          <el-input v-model="form.category" :placeholder="t('common.inputText')" />
        </el-form-item>
        <el-form-item label="单据编号" prop="enCode">
          <el-input v-model="form.enCode" :placeholder="t('common.inputText')" />
        </el-form-item>
        <el-form-item label="单据前缀" prop="prefix">
          <el-input v-model="form.prefix" :placeholder="t('common.inputText')" />
        </el-form-item>
        <el-form-item label="日期格式" prop="dateFormat">
          <el-input v-model="form.dateFormat" :placeholder="t('common.inputText')" />
        </el-form-item>
        <el-form-item label="流水位数" prop="digit">
          <el-input v-model="form.digit" :placeholder="t('common.inputText')" />
        </el-form-item>
        <el-form-item label="流水起始" prop="startNumber">
          <el-input v-model="form.startNumber" :placeholder="t('common.inputText')" />
        </el-form-item>
        <el-form-item label="流水范例" prop="example">
          <el-input v-model="form.example" :placeholder="t('common.inputText')" />
        </el-form-item>
        <el-form-item label="当前流水号" prop="thisNumber">
          <el-input v-model="form.thisNumber" :placeholder="t('common.inputText')" />
        </el-form-item>
        <el-form-item label="输出流水号" prop="outputNumber">
          <el-input v-model="form.outputNumber" :placeholder="t('common.inputText')" />
        </el-form-item>
        <el-form-item label="描述说明" prop="description">
            <el-input v-model="form.description" type="textarea" :placeholder="t('common.inputText')" />
        </el-form-item>
        <el-form-item label="排序" prop="seq">
          <el-input v-model="form.seq" :placeholder="t('common.inputText')" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">{{t('common.okText')}}</el-button>
          <el-button @click="cancel">{{t('common.cancelText')}}</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BillRule" lang="ts">
import { listBillRule, getBillRule, delBillRule, addBillRule, updateBillRule } from '@/api/system/billRule';
import { BillRuleVO, BillRuleQuery, BillRuleForm } from '@/api/system/billRule/types';
import { ComponentInternalInstance } from 'vue';
import { ElForm } from 'element-plus';
import {searchAnimate} from "@/animate";
import {useI18n} from '@/hooks/web/useI18n';
const { t } = useI18n();

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const billRuleList = ref<BillRuleVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref(ElForm);
const billRuleFormRef = ref(ElForm);

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: BillRuleForm = {
  id: undefined,
  name: undefined,
  category: undefined,
  enCode: undefined,
  prefix: undefined,
  dateFormat: undefined,
  digit: undefined,
  startNumber: undefined,
  example: undefined,
  thisNumber: undefined,
  outputNumber: undefined,
  description: undefined,
  seq: undefined,
}


                const  ruleMessageid=t('view.billRule-id')+t('common.notNullText');
                const  ruleMessagename=t('view.billRule-name')+t('common.notNullText');
                const  ruleMessagecategory=t('view.billRule-category')+t('common.notNullText');
                const  ruleMessageenCode=t('view.billRule-enCode')+t('common.notNullText');
                const  ruleMessageprefix=t('view.billRule-prefix')+t('common.notNullText');
                const  ruleMessagedateFormat=t('view.billRule-dateFormat')+t('common.notNullText');
                const  ruleMessagedigit=t('view.billRule-digit')+t('common.notNullText');
                const  ruleMessagestartNumber=t('view.billRule-startNumber')+t('common.notNullText');
                const  ruleMessageexample=t('view.billRule-example')+t('common.notNullText');
                const  ruleMessagethisNumber=t('view.billRule-thisNumber')+t('common.notNullText');
                const  ruleMessageoutputNumber=t('view.billRule-outputNumber')+t('common.notNullText');
                const  ruleMessagedescription=t('view.billRule-description')+t('common.notNullText');
                const  ruleMessageseq=t('view.billRule-seq')+t('common.notNullText');

const data = reactive<PageData<BillRuleForm, BillRuleQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    category: undefined,
    enCode: undefined,
    prefix: undefined,
    dateFormat: undefined,
    digit: undefined,
    startNumber: undefined,
    example: undefined,
    thisNumber: undefined,
    outputNumber: undefined,
    description: undefined,
    seq: undefined,
  },
  rules: {
    id: [
      { required: true, message: ruleMessageid, trigger: "blur" }
    ],
    name: [
      { required: true, message: ruleMessagename, trigger: "blur" }
    ],
    category: [
      { required: true, message: ruleMessagecategory, trigger: "blur" }
    ],
    enCode: [
      { required: true, message: ruleMessageenCode, trigger: "blur" }
    ],
    prefix: [
      { required: true, message: ruleMessageprefix, trigger: "blur" }
    ],
    dateFormat: [
      { required: true, message: ruleMessagedateFormat, trigger: "blur" }
    ],
    digit: [
      { required: true, message: ruleMessagedigit, trigger: "blur" }
    ],
    startNumber: [
      { required: true, message: ruleMessagestartNumber, trigger: "blur" }
    ],
    example: [
      { required: true, message: ruleMessageexample, trigger: "blur" }
    ],
    thisNumber: [
      { required: true, message: ruleMessagethisNumber, trigger: "blur" }
    ],
    outputNumber: [
      { required: true, message: ruleMessageoutputNumber, trigger: "blur" }
    ],
    description: [
      { required: true, message: ruleMessagedescription, trigger: "blur" }
    ],
    seq: [
      { required: true, message: ruleMessageseq, trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询单据规则列表 */
const getList = async () => {
  loading.value = true;
  const res = await listBillRule(queryParams.value);
  billRuleList.value = res.rows;
  total.value = parseInt(res.total);
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  billRuleFormRef.value.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: BillRuleVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  dialog.visible = true;
  dialog.title =  t('common.addText');
  nextTick(() => {
    reset();
  });
}

/** 修改按钮操作 */
const handleUpdate = (row?: BillRuleVO) => {
  loading.value = true
  dialog.visible = true;
  dialog.title = t('common.editText');
  nextTick(async () => {
    reset();
    const _id = row?.id || ids.value[0]
    const res = await getBillRule(_id);
    loading.value = false;
    Object.assign(form.value, res.data);
  });
}

/** 提交按钮 */
const submitForm = () => {
  billRuleFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateBillRule(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addBillRule(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess(t('common.editSuccessText'));
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: BillRuleVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm(t('common.delConfirmText')).finally(() => loading.value = false);
  await delBillRule(_ids);
  proxy?.$modal.msgSuccess(t('common.delSuccessText'));
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('system/billRule/export', {
    ...queryParams.value
  }, `billRule_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
